<template>
  <div class="bg">
    <div class="title">我的工具箱</div>
    <div class="form">
      <el-form
        :model="form"
        label-width="auto"
        :rules="rules"
        class="elForm"
        ref="elFormRef">
        <el-form-item>
          <div class="avatar">
            <el-avatar
              :size="80"
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
          </div>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" />
        </el-form-item>
        <el-form-item label="密码" prop="userPass">
          <el-input v-model="form.userPass" type="password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" class="login">
            登录
          </el-button>
        </el-form-item>
        <el-form-item>
          <div class="goRegister">
            <span @click="goRegister">还没帐号？点击注册</span>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="loginType">
            <img class="img" :src="getImageUrl('QQ.png')" @click="QQ_Login" />
            <img
              class="img"
              :src="getImageUrl('weixin.png')"
              @click="WEIXIN_Login" />
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { login } from "../../src/utils/https/api";
import { reactive, ref } from "vue";
import storage from "../storage/storage";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const router = useRouter();

let form = reactive({
  userName: "",
  userPass: "",
});

let elFormRef = ref();

const rules = reactive({
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  userPass: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

let onSubmit = async () => {
  await elFormRef.value?.validate((valid, fields) => {
    if (!valid) return;
    if (valid) {
      login(form).then((res) => {
        console.log(res);
        storage.setCookies("session", res.data.userInfo.session);
        storage.setCookies("userInfo", JSON.stringify(res.data.userInfo));
        ElMessage.success("登录成功");
        setTimeout(() => {
          router.push({
            path: "/index",
          });
        }, 100);
      });
    }
  });
};

let goRegister = () => {
  router.push({
    path: "/register",
  });
};

//QQ登录
let QQ_Login = () => {};

let getImageUrl = (name) => {
  // 请注意，这不包括子目录中的文件
  return new URL(`/src/image/${name}`, import.meta.url).href;
};
</script>
<style scoped>
.avatar {
  display: flex;
  justify-content: center;
  width: 100%;
}
.title {
  font-size: 4rem;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #7ed6df;
  padding: 5rem 0;
}

.form {
  display: flex;
  justify-content: center;
  align-items: center;
}

.elForm {
  width: 40%;
  background: url("@/image/from.png") no-repeat;
  background-size: 100% 100%;
  padding: 40px 30px 30px;
  border-radius: 20px;
}

.login {
  width: 100%;
  margin-top: 30px;
}

.goRegister {
  width: 100%;
  text-align: right;
  color: #0984e3;
  text-decoration-line: underline;
  cursor: pointer;
}
.loginType {
  width: 100%;
  display: flex;
  justify-content: center;
}
.img {
  width: 2rem;
  height: 2rem;
  margin: 0 1rem;
}
</style>
